package router

import (
	"dhuviewer/services"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func SubRoute(rg *gin.RouterGroup) {
	subGroup := rg.Group("/")
	subGroup.GET("/mine", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		res, err := services.ListSub(uuid.MustParse(payload.Uid))
		HelpErrorWithReturn(c, err, res)
	})

	subGroup.GET("/zoneAll", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		res, err := services.ListSubGallery(uuid.MustParse(payload.Uid))
		// fmt.Println(err)
		HelpErrorWithReturn(c, err, res)
	})

	subGroup.GET("/details/:uid", func(c *gin.Context) {
		_, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		userId := c.Param("uid")
		res, err := services.ListSubInfo(uuid.MustParse(userId))
		HelpErrorWithReturn(c, err, res)
	})

	subGroup.POST("/init", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		err = services.CreateSub(uuid.MustParse(payload.Uid))
		HelpErrorWithCode(c, err)
	})

	subGroup.POST("/addUid", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		json := make(map[string]interface{})
		c.BindJSON(&json)
		temp := json["targetUid"].(string)
		// err = services.Add2Sub(uuid.MustParse(payload.Uid), uuid.MustParse(temp))
		err = services.AddSubWithCommit(uuid.MustParse(payload.Uid), uuid.MustParse(temp))
		HelpErrorWithCode(c, err)
	})

	subGroup.DELETE("/removeUid", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		json := make(map[string]interface{})
		c.BindJSON(&json)
		temp := json["targetUid"].(string)
		// err = services.DelOneSub(uuid.MustParse(payload.Uid), uuid.MustParse(temp))
		err = services.DelSubWithCommit(uuid.MustParse(payload.Uid), uuid.MustParse(temp))
		HelpErrorWithCode(c, err)
	})
}
